cmake_minimum_required(VERSION 3.10)

#绕过检查
SET(CMAKE_C_COMPILER_WORKS TURE)
SET(CMAKE_CXX_COMPILER_WORKS TURE)

project(simple_foc)

ENABLE_LANGUAGE(ASM C)

add_definitions(
-DSTM32F103xB
-DUSE_HAL_DRIVER
)

set(MCU_FLAGS "-mcpu=cortex-m3 -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -mfloat-abi=soft -ffunction-sections -fdata-sections")

set(CMAKE_ASM_FLAGS "-c ${MCU_FLAGS} -x assembler-with-cpp -Wa,-mimplicit-it=thumb -gdwarf-2")

set(CMAKE_C_FLAGS "${MCU_FLAGS} -Dgcc -w -Wno-unknown-pragmas -O0 -gdwarf-2 -g")
set(CMAKE_C_FLAGS_DEBUG "-O0 -gdwarf-2 -g")
set(CMAKE_C_FLAGS_RELEASE "-O3")

set(LINKER_SCRIPT STM32F103xB_FLASH.ld)

set(CMAKE_EXE_LINKER_FLAGS "--specs=nano.specs -specs=nosys.specs -T${LINKER_SCRIPT} -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections")
# set(CMAKE_EXE_LINKER_FLAGS "${MCU_FLAGS} -Wl,--gc-sections,-Map=${PROJECT_NAME}.map,--cref,-u,Reset_Handler -T${LINKER_SCRIPT}")
# set(CMAKE_EXE_LINKER_FLAGS "${MCU_FLAGS} --specs=nano.specs -specs=nosys.specs -Wl,--gc-sections,-Map=${PROJECT_NAME}.map,--cref,-u,Reset_Handler -T${LINKER_SCRIPT}")

set(SRC_PATH ../../src)

include_directories(${SRC_PATH}/App)
include_directories(${SRC_PATH}/Bsp)
include_directories(${SRC_PATH}/Common)
include_directories(${SRC_PATH}/Common/ringbuff)
include_directories(${SRC_PATH}/CMSIS/Include)
include_directories(${SRC_PATH}/CMSIS/Device/ST/STM32F1xx/Include)
include_directories(${SRC_PATH}/STM32F1xx_HAL_Driver/Inc)
include_directories(${SRC_PATH}/STM32F1xx_HAL_Driver/Inc/Legacy)
include_directories(${SRC_PATH}/rtthread/include)
include_directories(${SRC_PATH}/rtthread/components/finsh)


# file(GLOB APP_SRC ../../src/app/*.c)

set(CMSIS_SRC 
${SRC_PATH}/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/startup_stm32f103xb.s
${SRC_PATH}/CMSIS/Device/ST/STM32F1xx/Source/Templates/system_stm32f1xx.c
)

set(STM32F1xx_HAL_Driver_SRC
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c
${SRC_PATH}/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_can.c
)

set(RTThread_SRC 
${SRC_PATH}/rtthread/libcpu/arm/cortex-m3/context_gcc.S
${SRC_PATH}/rtthread/libcpu/arm/cortex-m3/cpuport.c

${SRC_PATH}/rtthread/src/clock.c
${SRC_PATH}/rtthread/src/components.c
${SRC_PATH}/rtthread/src/cpu.c
${SRC_PATH}/rtthread/src/idle.c
${SRC_PATH}/rtthread/src/ipc.c
${SRC_PATH}/rtthread/src/irq.c
${SRC_PATH}/rtthread/src/kservice.c
${SRC_PATH}/rtthread/src/mem.c
${SRC_PATH}/rtthread/src/memheap.c
${SRC_PATH}/rtthread/src/mempool.c
${SRC_PATH}/rtthread/src/object.c
${SRC_PATH}/rtthread/src/scheduler.c
${SRC_PATH}/rtthread/src/slab.c
${SRC_PATH}/rtthread/src/thread.c
${SRC_PATH}/rtthread/src/timer.c
)

set(FinSH_SRC 
${SRC_PATH}/rtthread/components/finsh/cmd.c
${SRC_PATH}/rtthread/components/finsh/finsh_port.c
${SRC_PATH}/rtthread/components/finsh/msh.c
${SRC_PATH}/rtthread/components/finsh/shell.c

)

set(Bsp_SRC 
${SRC_PATH}/Bsp/board.c
${SRC_PATH}/Bsp/stm32f1xx_hal_msp.c
${SRC_PATH}/Bsp/stm32f1xx_it.c
${SRC_PATH}/Bsp/gpio.c
${SRC_PATH}/Bsp/usart.c
${SRC_PATH}/Bsp/can.c
${SRC_PATH}/Bsp/tim.c

)

set(Common_SRC 


)

set(App_SRC 
${SRC_PATH}/App/main.c

)

add_executable(${PROJECT_NAME}.elf 
    ${CMSIS_SRC}
    ${STM32F1xx_HAL_Driver_SRC}
    ${RTThread_SRC}
    ${FinSH_SRC}
    ${Common_SRC}
    ${Bsp_SRC}
    ${App_SRC}
)


# add_library(platform
# PLATFORM_SRC
# )

# add_library(stm32l4xx_hal
# STM32L4xx_HAL_SRC
# )


# target_link_libraries(${PROJECT_NAME}.elf
#     ${PROJECT_NAME}
# )

set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf)
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -Obinary ${ELF_FILE} ${BIN_FILE}
    COMMAND ${CMAKE_OBJCOPY} -Oihex  ${ELF_FILE} ${HEX_FILE}

    COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
)
